package com.ruoyi.web.controller.danger;
import cn.hutool.core.collection.CollUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.opio.util.ExampleBase;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.entity.SafetyInstrument;
import com.ruoyi.system.service.SafetyInstrumentService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 安全仪表基础点位信息(SafetyInstrument)表控制层
 * @since 2023-04-21 14:59:45
 */
@RestController
@RequestMapping("safetyInstrument")
public class SafetyInstrumentController extends BaseController {

    @Resource
    private SafetyInstrumentService safetyInstrumentService;

    /**
     * 分页查询
     * @return 查询结果
     */
    @GetMapping("/list")
    public TableDataInfo queryByPage(SafetyInstrument safetyInstrument) {
        startPage();
        List<SafetyInstrument> sysDangerPoints = safetyInstrumentService.queryByPage(safetyInstrument);
        return getDataTable(sysDangerPoints);
    }

    /**
     * 通过主键查询单条数据
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public AjaxResult queryById(@PathVariable("id") Integer id) {
        return AjaxResult.success(this.safetyInstrumentService.queryById(id));
    }

    /**
     * 新增数据
     * @param safetyInstrument 实体
     * @return 新增结果
     */
    @PostMapping("/create")
    @PreAuthorize("@ss.hasPermi('safetyInstrument:create')")
    public AjaxResult add(@RequestBody SafetyInstrument safetyInstrument) {
        return AjaxResult.success(this.safetyInstrumentService.insert(safetyInstrument));
    }

    /**
     * 编辑数据
     * @param safetyInstrument 实体
     * @return 编辑结果
     */
    @PostMapping("/update")
    @PreAuthorize("@ss.hasPermi('safetyInstrument:update')")
    public AjaxResult edit(@RequestBody SafetyInstrument safetyInstrument) {
        return AjaxResult.success(this.safetyInstrumentService.update(safetyInstrument));
    }

    /**
     * 删除数据
     * @param id 主键
     * @return 删除是否成功
     */
    @GetMapping("/delete")
    @PreAuthorize("@ss.hasPermi('safetyInstrument:delete')")
    public AjaxResult deleteById(Integer id) {
        return AjaxResult.success(this.safetyInstrumentService.deleteById(id));
    }


    /**
     * 与实时数据库关联
     * @return
     */
    @GetMapping("/getTimeData")
    public AjaxResult getTimeData(SafetyInstrument query) {
        List<SafetyInstrument> safetyInstruments = this.safetyInstrumentService.queryByPage(query);
        Map<String, Object> map = ExampleBase.toResult();
        if (CollUtil.isNotEmpty(map)) {
            safetyInstruments.forEach(safetyInstrument -> {
                if (map.containsKey(safetyInstrument.getInterlockActionNumber())) {
                    Object o = map.get(safetyInstrument.getInterlockActionNumber());
                    safetyInstrument.setAvTwo(o.toString());
                }
                if (map.containsKey(safetyInstrument.getInterlockNumber())) {
                    Object o = map.get(safetyInstrument.getInterlockNumber());
                    safetyInstrument.setAv(o.toString());
                }
            });
        }
        return AjaxResult.success(safetyInstruments);
    }


    /**
     * 导入
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @Log(title = "安全仪表点位(", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception{
        ExcelUtil<SafetyInstrument> util = new ExcelUtil<>(SafetyInstrument.class);
        List<SafetyInstrument> safetyInstruments = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = safetyInstrumentService.importExcel(safetyInstruments, updateSupport, operName);
        return success(message);
    }


    /**
     * 导出
     * @param response
     * @param safetyInstrument
     */
    @Log(title = "安全仪表点位(", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SafetyInstrument safetyInstrument){
        List<SafetyInstrument> safetyInstruments = safetyInstrumentService.queryByPage(safetyInstrument);
        ExcelUtil<SafetyInstrument> util = new ExcelUtil<>(SafetyInstrument.class);
        util.exportExcel(response, safetyInstruments, "安全仪表点位(");
    }


    /**
     *
     * @param response
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<SafetyInstrument> util = new ExcelUtil<>(SafetyInstrument.class);
        util.importTemplateExcel(response,"安全仪表点位(");
    }

}

